package com.lovegu.three;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 基于 JavaFx 实现黑白名单
 * </p>
 *
 * @author 老顾
 * @since 2023/4/5
 */
public class BlackWhiteListApp extends Application {

    private final List<String> blackList = new ArrayList<>();
    private final List<String> whiteList = new ArrayList<>();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("黑白名单");

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        Label blackListLabel = new Label("黑名单");
        grid.add(blackListLabel, 0, 0);

        Label whiteListLabel = new Label("白名单");
        grid.add(whiteListLabel, 1, 0);

        ListView<String> blackListView = new ListView<>();
        blackListView.setPrefWidth(150);
        blackListView.setPrefHeight(150);
        grid.add(blackListView, 0, 1);

        ListView<String> whiteListView = new ListView<>();
        whiteListView.setPrefWidth(150);
        whiteListView.setPrefHeight(150);
        grid.add(whiteListView, 1, 1);

        TextField nameTextField = new TextField();
        grid.add(nameTextField, 0, 2);

        ToggleGroup group = new ToggleGroup();

        RadioButton blackRadio = new RadioButton("黑名单");
        blackRadio.setToggleGroup(group);
        blackRadio.setSelected(true);
        grid.add(blackRadio, 1, 2);

        RadioButton whiteRadio = new RadioButton("白名单");
        whiteRadio.setToggleGroup(group);
        grid.add(whiteRadio, 2, 2);

        Button addButton = new Button("添加");
        addButton.setOnAction(event -> {
            String name = nameTextField.getText().trim();
            if (!name.isEmpty()) {
                if (blackRadio.isSelected()) {
                    if (!blackList.contains(name)) {
                        blackList.add(name);
                        blackListView.getItems().add(name);
                    }
                } else {
                    if (!whiteList.contains(name)) {
                        whiteList.add(name);
                        whiteListView.getItems().add(name);
                    }
                }
                nameTextField.clear();
            }
        });
        grid.add(addButton, 0, 3);

        Button removeButton = new Button("移除");
        removeButton.setOnAction(event -> {
            String name = null;
            if (blackRadio.isSelected()) {
                name = blackListView.getSelectionModel().getSelectedItem();
                blackList.remove(name);
                blackListView.getItems().remove(name);
            } else {
                name = whiteListView.getSelectionModel().getSelectedItem();
                whiteList.remove(name);
                whiteListView.getItems().remove(name);
            }
        });
        grid.add(removeButton, 1, 3);

        Button clearButton = new Button("清空");
        clearButton.setOnAction(event -> {
            blackList.clear();
            blackListView.getItems().clear();
            whiteList.clear();
            whiteListView.getItems().clear();
        });
        grid.add(clearButton, 2, 3);

        Button exportButton = new Button("导出");
        exportButton.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("导出黑白名单");
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("文本文件", "*.txt"),
                    new FileChooser.ExtensionFilter("所有文件", "*.*")
            );
            File file = fileChooser.showSaveDialog(primaryStage);
            if (file != null) {
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
                    writer.write("黑名单：\n");
                    for (String name : blackList) {
                        writer.write(name + "\n");
                    }
                    writer.write("白名单：\n");
                    for (String name : whiteList) {
                        writer.write(name + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        grid.add(exportButton, 0, 4);

        Button importButton = new Button("导入");
        importButton.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("导入黑白名单");
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("文本文件", "*.txt"),
                    new FileChooser.ExtensionFilter("所有文件", "*.*")
            );
            File file = fileChooser.showOpenDialog(primaryStage);
            if (file != null) {
                try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        if (line.equals("黑名单：")) {
                            while ((line = reader.readLine()) != null && !line.equals("白名单：")) {
                                if (!blackList.contains(line)) {
                                    blackList.add(line);
                                    blackListView.getItems().add(line);
                                }
                            }
                        } else if (line.equals("白名单：")) {
                            while ((line = reader.readLine()) != null) {
                                if (!whiteList.contains(line)) {
                                    whiteList.add(line);
                                    whiteListView.getItems().add(line);
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        grid.add(importButton, 1, 4);

        TextField queryTextField = new TextField();
        grid.add(queryTextField, 0, 5);

        Button queryButton = new Button("查询");
        queryButton.setOnAction(event -> {
            String query = queryTextField.getText().trim();
            if (!query.isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("查询结果");
                alert.setHeaderText("查询结果如下：");
                StringBuilder sb = new StringBuilder();
                sb.append("黑名单：\n");
                for (String name : blackList) {
                    if (name.contains(query)) {
                        sb.append(name).append("\n");
                    }
                }
                sb.append("白名单：\n");
                for (String name : whiteList) {
                    if (name.contains(query)) {
                        sb.append(name).append("\n");
                    }
                }
                alert.setContentText(sb.toString());
                alert.showAndWait();
                queryTextField.clear();
            }
        });
        grid.add(queryButton, 1, 5);

        Scene scene = new Scene(grid, 500, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}